﻿package com.mixmedia.net {
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;	

	/**
	 * @author colin
 * responsible to create a proxy movieclip
 */
	public class LoadSWF extends EventDispatcher implements ILoader, IFASTEventDispatcher {
		private var mcImageLoader:Loader;
	
		private var autoStart:Boolean = true;
	
		public function LoadSWF(target:DisplayObjectContainer,autoStart:Boolean=true){
			if(target==undefined)trace('com.mixmedia.net.LoadSWF require target movieclip');

			this.autoStart = autoStart;
			mcImageLoader = new Loader();
			target.addChild(mcImageLoader);

			mcImageLoader.loaderInfo.addEventListener(LoaderEvent.READY, onReady);
			mcImageLoader.loaderInfo.addEventListener(LoaderEvent.COMPLETE, onComplete);
		}

		public function load(requestURL:String):void {
			mcImageLoader.unload();
			isComplete = false;
			mcImageLoader.load(new URLRequest(requestURL));
		}
	
		private var isComplete:Boolean = false;
		private function onComplete():void{
			isComplete=true;
			if(autoStart){
				if(mcImageLoader.content is MovieClip){
					MovieClip(mcImageLoader.content).play();
				}
			}
		}
	
		private function onReady():void{
			if(isComplete==true)return;
			if(mcImageLoader.content is MovieClip){
				MovieClip(mcImageLoader.content).stop();
			}
		}

		public function getTargetContainer() : Object {
			return mcImageLoader;
		}
	
		public function unload() : void {
			mcImageLoader.unload();
		}
	
		public function when(eventType : String, whichObject : Object, callFunction : Function) : void {
			addEventListener(eventType, callFunction, false, 0, true);
		}
	}
}